package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mp.approvalreq.entity.Email;
import com.mp.approvalreq.service.SubmitRequestServicePrx;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 获取审批导出邮箱
 */
@WebServlet("/getExportEmail")
public class GetExportEmail extends AbstractServlet {
	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = LoggerFactory.getLogger(GetExportEmail.class);
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		try {
			User user = getReqUser(req);
			
			SubmitRequestServicePrx submitRequestServicePrx = ServiceUtils.getSubmitRequestServicePrx();
			Email email = submitRequestServicePrx.getExportEmail(user);
			
            returnResponse(resp, email);
		} catch (Throwable e) {
			logger.error(e.getMessage(), e);
			onThrowable(req, resp, e);
		}
	}

    private void returnResponse(HttpServletResponse resp, Email email) throws Throwable {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", "0");
        jsonObject.put("res_info", "成功");
        jsonObject.put("email", email);
	
	    // 对象里面有个字段名也叫email
        logger.info(JSON.toJSONString(jsonObject,  SerializerFeature.DisableCircularReferenceDetect));
		
	    printJson(resp, jsonObject.toJSONString());
    }


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
